package com.dut.bbs.adapter;

import java.util.HashMap;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.dut.bbs.R;

public class FaceAdapter extends BaseAdapter {   
    
	private Context context;
	private String[] faceNames;
	private HashMap<String, Integer> faces;

	public FaceAdapter(Context context, String[] faceName) {
		this.context = context;
		this.faceNames = faceName;
		this.faces = Face.getfaces(context);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getCount()
	 */
	public int getCount() {
		return faceNames == null ? 0 : faceNames.length;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItem(int)
	 */
	public Object getItem(int position) {
		return position;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getItemId(int)
	 */
	public long getItemId(int position) {
		return position;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.widget.Adapter#getView(int, android.view.View,
	 * android.view.ViewGroup)
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView ivImageView = null;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.faceitem, null);
			ivImageView = (ImageView) convertView
					.findViewById(R.id.imageview_iv);
			convertView.setTag(ivImageView);
		} else {
			ivImageView = (ImageView) convertView.getTag();
		}
		if (faces.containsKey(Face.faceNames[position])) {
			ivImageView.setImageResource(faces.get(Face.faceNames[position]));
		}
		return convertView;
	}
}  
